home *** CD-ROM | disk | FTP | other *** search
- Path: newshost.lanl.gov!tanmoy
- From: tanmoy@qcd.lanl.gov (Tanmoy Bhattacharya)
- Newsgroups: comp.lang.c
- Subject: Re: sscanf bug??????
- Date: 10 Feb 1996 23:24:58 GMT
- Organization: Los Alamos National Laboratory
- Message-ID: <TANMOY.96Feb10162458@qcd.lanl.gov>
- References: <4fimvo$82s@fnord.dfw.net>
- NNTP-Posting-Host: qcd.lanl.gov
- Mime-Version: 1.0
- Content-Type: text
- In-reply-to: jtmcap@dfw.dfw.net's message of 10 Feb 1996 18:08:56 GMT
-
- In article <4fimvo$82s@fnord.dfw.net> jtmcap@dfw.dfw.net (Jerry
- Jackson) writes:
- <snip>
- #include <stdio.h>
- #include <string.h>
-
- main()
- {
- char str_1[] = "013196";
- char str_2[] = "13196";
- long res_1, res_2;
-
- sscanf(str_1,"%d",&res_1);
- sscanf(str_2,"%d",&res_2);
-
- printf("\nres_1 = %d",res_1);
- printf("\nres_2 = %d",res_2);
- }
-
- the output looks like this:
-
- res_1 = 89
- res_2 = 13196
-
-
- microway says that the leading zero causes sscanf to do an octal
- conversion on the integer. i have not found any documentation to verify
- this. also other compilers that i use return the value 13196 for both
- calls to sscanf.
-
- bug or undocumented feature?
-
- Difficult to say. Your code invokes undefined behaviour: %d expects an
- int*, but you are passing it a long*. Once you invoke undefined
- behaviour, anything is allowed to happen. Similarly, in the printf, %d
- expects an int and you are passing it a long.
-
- However, microway is definitely wrong. If after changing the
- declaration of res1 and res2, your result persists; the compiler is
- buggy beyond doubt.
-
- Note that if you use %i in the sscanf, you are supposed to get the
- behaviour you see.
-
- Cheers
- Tanmoy
- --
- tanmoy@qcd.lanl.gov(128.165.23.46) DECNET: BETA::"tanmoy@lanl.gov"(1.218=1242)
- Tanmoy Bhattacharya O:T-8(MS B285)LANL,NM87545 H:#9,3000,Trinity Drive,NM87544
- Others see <gopher://yaleinfo.yale.edu:7700/00/Internet-People/internet-mail>,
- <http://alpha.acast.nova.edu/cgi-bin/inmgq.pl>or<ftp://csd4.csd.uwm.edu/pub/
- internetwork-mail-guide>. -- <http://nqcd.lanl.gov/people/tanmoy/tanmoy.html>
- fax: 1 (505) 665 3003 voice: 1 (505) 665 4733 [ Home: 1 (505) 662 5596 ]
-